<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" >

<head>
    <meta charset="UTF-8">
    <title>广告列表</title>
</head>
<script type="text/javascript" th:src="@{/js/jquery-2.1.4.min.js}"></script>
<script type="text/javascript" th:src="@{/js/bootstrap-3.3.7-dist/js/bootstrap.min.js}"></script>
<link type="text/css" rel="stylesheet" th:href="@{/js/bootstrap-3.3.7-dist/css/bootstrap.min.css}">
<script type="text/javascript" th:src="@{/js/bootstrap-treeview/bootstrap-treeview.min.js}"></script>
<link type="text/css" rel="stylesheet" th:href="@{/js/bootstrap-treeview/bootstrap-treeview.min.css}">
<script type="text/javascript" th:src="@{/js/bootStrap-addTabs/bootstrap.addtabs.min.js}"></script>
<link type="text/css" rel="stylesheet" th:href="@{/js/bootStrap-addTabs/bootstrap.addtabs.css}">
<script type="text/javascript" th:src="@{/js/bootstrap-table/bootstrap-table.min.js}"></script>
<script type="text/javascript" th:src="@{/js/bootstrap-table/locale/bootstrap-table-zh-CN.min.js}"></script>
<link type="text/css" rel="stylesheet" th:href="@{/js/bootstrap-table/bootstrap-table.min.css}">
<link type="text/css" rel="stylesheet" th:href="@{/js/bootstrap-fileinput/css/fileinput.css}">

<script type="text/javascript" th:src="@{/js/bootstrap-bootbox/bootbox.js}"></script>
<script type="text/javascript" th:src="@{/js/bootstrap-fileinput/js/fileinput.min.js}"></script>
<script th:src="@{/js/bootstrap-fileinput/js/locales/zh.js}"></script>

<body>
<!-- 修改模态框（Modal） -->
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel1">修改广告</h4>
            </div>
            <div class="modal-body">
                <form id="myForm1">
                 <input type=”hidden” class="form-control" name="advert_id" id="product_id">
                    广告名称：<input type="text" class="form-control" name="advert_name"id="product_name"   placeholder="广告名称">
                    广告描述：  <input type="text" class="form-control" name="advert_description" id="product_price" placeholder="广告描述">
                    广告数量：<input type="text" class="form-control" name="advert_num" id="product_num" placeholder="广告数量">
                    广告图片：<input id="file-Portrait" name="img" class="form-control" type="file">

                </form>
            </div>
            <!--  ====================================================   -->

            <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-default" type="button">关闭</button>
                <button class="btn btn-primary" type="button" onclick="sub()">确认修改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!--搜索 -->
<div id="tb">

    广告名称：<input type="text" class="form-control"  id="name" name="advert_name" placeholder="请输入广告名称">
    广告数量：<input type="text" class="form-control"  id="maxprice" name="maxnum" placeholder="请输入数量">
    广告数量：<input type="text" class="form-control"  id="minprice"name="minnum" placeholder="请输入数量">
    <button type="button" onclick="sousuo()" class="btn btn-info">搜索</button>

    <button type="button" class="btn btn-primary"
            data-toggle="modal" data-target="#addLayer">添加广告</button>
</div>




<!--记录添加弹出层 -->
<div class="modal fade" id="addLayer" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">

                <button data-dismiss="modal" class="close" type="button"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">添加广告</h4>

            </div>
            <!--=========================================================================================  -->
            <div class="modal-body">
                <form id="myForm">
                    广告名称：<input type="text" class="form-control" name="advert_name"   placeholder="广告名称">
                    广告描述：  <input type="text" class="form-control" name="advert_description"  placeholder="广告描述">
                    广告数量：<input type="text" class="form-control" name="advert_num"  placeholder="广告数量">
                    广告图片：<input  name="img" class="form-control" type="file">
                </form>
            </div>
            <!--=========================================================================================  -->
            <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-default" type="button">关闭</button>
                <button class="btn btn-primary" type="button" onclick="add()">确认添加</button>
            </div>

        </div>
    </div>
</div>



<!-- 查询列表-->
<table id="myTable"></table>

</body>

<script >
    function sousuo(){
        $("#myTable").bootstrapTable("refresh", {
            "query": {
                advert_name: $("#name").val(),
                maxnum: $("#maxprice").val(),
                minnum: $("#minprice").val(),


            }
        });
    }

    $("#myTable").bootstrapTable({
        url:"getListAdv",
        columns:[
            {field:"xuan",checkbox:true},
            {field:"advert_id",title:"编号"},
            {field:"advert_name",title:"广告名称"},
            {field:"advert_description",title:"广告描述"},

            {field:"advert_pic",title:"广告图片",formatter:function(value,row,index){
                    return "<img width='50px' height='50px' src='"+value+"'>";
                }},
            {field:"advert_num",title:"投放数量"},
            {field:"cxz",title:"操作",formatter:function(value,row){
                    var str='<button class="btn btn-info" onclick="del('+row.advert_id+')">删除</button>';
                    var str2='<button class="btn btn-primary" onclick="getProId('+row.advert_id+')" data-toggle="modal" data-target="#myModal1">修改广告</button>';
                    return str+"   "+str2;
                }},
        ],
        pagination:true,
        pageSize:10,
        pageList:[2,4,6,8,10],
        sidePagination: "client",
        toolbar:"#tb",
    })

    //修改回显
    function getProId(advert_id){
        $.ajax({
            url:"AdvById",
            data:{id:advert_id},
            type:"post",
            dataType:"json",
            success:function(data){
                $("#product_id").val(data.advert_id);
                $("#product_name").val(data.advert_name);
                $("#product_price").val(data.advert_description);
                $("#file-Portrait").fileinput('refresh', {
                    initialPreview: "<img src="+data.advert_pic+" height='180px' width='150px' class='file-preview-image'>",
                });

                $("#product_num").val(data.advert_num);



            }
        })
    }
    //修改
    function sub(){
        $.ajax({
            url:"updAdv",
            type:"post",
            data:$("#myForm1").serialize(),
            success:function(){
                $('#myTable').bootstrapTable('refresh');//刷新表格
                $("#myModal1").modal('hide');

            },error:function(){
                $('#myTable').bootstrapTable('refresh');//刷新表格
                $("#myModal1").modal('hide');
            }
        })
    }
    //图片上传
    $('#file-Portrait').fileinput({
        language: 'zh', //设置语言
        uploadUrl: "upload", //上传的地址
        allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀,
        maxFileCount: 100,
        enctype: 'multipart/form-data',
        showUpload: true, //是否显示上传按钮
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",

        //dropZoneEnabled: false,//是否显示拖拽区域
        //minImageWidth: 50, //图片的最小宽度
        //minImageHeight: 50,//图片的最小高度
        //maxImageWidth: 1000,//图片的最大宽度
        //maxImageHeight: 1000,//图片的最大高度
        //maxFileSize: 0,//单位为kb，如果为0表示不限制文件大小
        //minFileCount: 0,
//     maxFileCount: 10, //表示允许同时上传的最大文件个数
//     enctype: 'multipart/form-data',
//     validateInitialCount:true,
//     previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
//     msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
    });
    //删除
    function del(advert_id){
        $.ajax({
            url:"delAdv",
            data:{id:advert_id},
            success:function(){
                //bootbox.confirm("成功", "info")
                $("#myTable").bootstrapTable('refresh');
            }
        })
    }

    function add(){
        $.ajax({
            url:"addAdv",
            type:"post",
            data:$("#myForm").serialize(),
            dataType:"json",
            success:function(){
                alert("成功")
                $('#myTable').bootstrapTable('refresh');//刷新表格
                $("#addLayer").modal('hide');
            },error:function(){
                alert("成功")
                $('#myTable').bootstrapTable('refresh');//刷新表格
                $("#addLayer").modal('hide'); //关闭弹窗
            }
        })
    }


</script>
</html>